package guoanmaker.operator.business.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import guoanmaker.operator.business.model.InRoundPhone;
import guoanmaker.operator.business.model.OperatorCoupons;
import guoanmaker.operator.business.model.OperatorServerClass;
import guoanmaker.operator.business.model.OperatorUserSignIn;
import guoanmaker.operator.business.model.OperatorUserSignInActive;
import guoanmaker.operator.business.model.OperatorUserSignInConfiguration;
import guoanmaker.operator.business.model.OperatorUserSignInSwitch;
import guoanmaker.operator.business.model.PersonalAddress;
import guoanmaker.operator.business.repository.OperatorServerClassRepository;
import guoanmaker.operator.business.service.OperatorCouponsService;
import guoanmaker.operator.business.service.OperatorUserSignInServiceIntegerface;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(ApiController.OPERATOR_PC_OFICIAL)
public class OperatorUserSignInController {
	@Autowired
	private  AjaxJson ajaxJson;
	@Autowired
	private OperatorUserSignInServiceIntegerface operatorUserSignInServiceIntegerface;
	@Autowired
	private OperatorCouponsService operatorCouponsService;
	@Autowired
	private OperatorServerClassRepository operatorServerClassRepository;
	@ApiOperation(value = "签到接口", notes = "签到接口")
	@RequestMapping(value = "/getsignin", method = RequestMethod.POST)
	public AjaxJson getsignin(String userid){
		try{
		Map remap=operatorUserSignInServiceIntegerface.selectUserSignIn(userid);
		ajaxJson.setSuccess(true).setStatus(200).setData(remap).setMsg(null);
		}catch(Exception e){
			e.printStackTrace();
    		ajaxJson.setSuccess(false).setStatus(250).setData(null).setMsg(null);
		}
	    return ajaxJson;
	}
	
	 @ApiOperation(value = "保存签到配置和签到活动", notes = "新增/修改保存签到配置和签到活动")
	    @PostMapping(value = "saveOperatorUserSignInConfiguration")
	 @Transactional
	    public AjaxJson saveOperatorUserSignInConfiguration(@RequestBody InRoundPhone inRoundPhone) {
	    	try{
	    		int a=0;
	    		int b=0;
	    		OperatorUserSignInActive operatorUserSignInActive=inRoundPhone.getOperatorUserSignInActive();
	    		operatorUserSignInActive.setCreateDate(new Date());
	    		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");  
	    		SimpleDateFormat sdf1  =   new  SimpleDateFormat( "yyyyMMddHHmmss" );
	    		String endstr=sdf.format(operatorUserSignInActive.getSignInEndTime());
	    		endstr=endstr+"235959";
	    		Date enddate = sdf1.parse(endstr);
	    		List<OperatorUserSignInActive> acs=operatorUserSignInServiceIntegerface.selectOperatorUserSignAcByDate(operatorUserSignInActive.getSignInCreateTime());
	    		if(acs.size()>0){
	    			System.out.println(operatorUserSignInActive.getSignInCreateTime());
	    			OperatorUserSignInActive signactivee=acs.get(0);
	    			long starttimea=signactivee.getSignInEndTime().getTime();
	    			long starttimeb=operatorUserSignInActive.getSignInCreateTime().getTime();//新建立的活动开始时间
	    			
	    			if(starttimeb<starttimea){
	    				
	    					a=1;//设定的开始时间小于以前建的开始时间，结束时间大于以前活动的最小开始时间，不能建立
	    					
	    				
	    			}else{
	    				operatorUserSignInActive.setSignInEndTime(enddate);
	    	    		OperatorUserSignInActive signactive=operatorUserSignInServiceIntegerface.saveOperatorUserSignInActive(operatorUserSignInActive);
	    	    		OperatorUserSignInConfiguration[] OperatorUserSignInConfigurations=inRoundPhone.getOperatorUserSignInConfigurations();
	    	    	
	    	    		operatorUserSignInServiceIntegerface.delectAllUserSignInConfiguration(signactive.getId());
	    	    	for(OperatorUserSignInConfiguration signconfig:OperatorUserSignInConfigurations){
	    	    		signconfig.setSignActiveId(signactive.getId());
	    	    		if(signconfig.getPrizeType()==1){
	    	    		OperatorCoupons operatorCoupons=operatorCouponsService.findbyCouponinfo(signconfig.getCouponsId());
	    	    		signconfig.setPrizeName("满"+operatorCoupons.getFullamount()  +"减"+operatorCoupons.getSubamount() +"券");
	    	    		signconfig.setFullamount(operatorCoupons.getFullamount());
	    	    		signconfig.setSubamount(operatorCoupons.getSubamount());
	    	    		if(!operatorCoupons.getServerid().equals("0")){
							OperatorServerClass operatorServerClass=operatorServerClassRepository.findOne(operatorCoupons.getServerid());
							signconfig.setServerName("仅限"+operatorServerClass.getServerName()+"类使用");
							}else{
								signconfig.setServerName("全平台均可使用");
							}
	    	    		if (operatorCoupons.getDatetype().equals("1") == true) {
	    	    			signconfig.setEndofdate(operatorCoupons.getEndofdate());
						} else {
							Date expiredDate = new Date();
							Calendar ca = Calendar.getInstance();
							ca.add(Calendar.DATE, operatorCoupons.getValidityPeriod());// num为增加的天数，可以改变的
							expiredDate = ca.getTime();
							signconfig.setEndofdate(expiredDate);
						}
	    	    		}
	    	    		operatorUserSignInServiceIntegerface.saveOperatorUserSignInConfiguration(signconfig);
	    	    	}
	    			}
	    			
	    			
	    			
	    		}else{
	    		System.out.println(endstr);
	    		System.out.println(enddate);
	    		operatorUserSignInActive.setSignInEndTime(enddate);
	    		OperatorUserSignInActive signactive=operatorUserSignInServiceIntegerface.saveOperatorUserSignInActive(operatorUserSignInActive);
	    		OperatorUserSignInConfiguration[] OperatorUserSignInConfigurations=inRoundPhone.getOperatorUserSignInConfigurations();
	    	
	    		operatorUserSignInServiceIntegerface.delectAllUserSignInConfiguration(signactive.getId());
	    	for(OperatorUserSignInConfiguration signconfig:OperatorUserSignInConfigurations){
	    		signconfig.setSignActiveId(signactive.getId());
	    		if(signconfig.getPrizeType()==1){
    	    		OperatorCoupons operatorCoupons=operatorCouponsService.findbyCouponinfo(signconfig.getCouponsId());
    	    		signconfig.setPrizeName("满"+operatorCoupons.getFullamount()  +"减"+operatorCoupons.getSubamount() +"券");
    	    		signconfig.setFullamount(operatorCoupons.getFullamount());
    	    		signconfig.setSubamount(operatorCoupons.getSubamount());
    	    		if(!operatorCoupons.getServerid().equals("0")){
						OperatorServerClass operatorServerClass=operatorServerClassRepository.findOne(operatorCoupons.getServerid());
						signconfig.setServerName("仅限"+operatorServerClass.getServerName()+"类使用");
						}else{
							signconfig.setServerName("全平台均可使用");
						}
    	    		if (operatorCoupons.getDatetype().equals("1") == true) {
    	    			signconfig.setEndofdate(operatorCoupons.getEndofdate());
					} else {
						Date expiredDate = new Date();
						Calendar ca = Calendar.getInstance();
						ca.add(Calendar.DATE, operatorCoupons.getValidityPeriod());// num为增加的天数，可以改变的
						expiredDate = ca.getTime();
						signconfig.setEndofdate(expiredDate);
					}
    	    		}
	    		operatorUserSignInServiceIntegerface.saveOperatorUserSignInConfiguration(signconfig);
	    	}
	    	
	    		}
	    	ajaxJson.setSuccess(true).setStatus(200).setData(a).setMsg(null);
	    	}catch(Exception e){
	    		e.printStackTrace();
	    		ajaxJson.setSuccess(false).setStatus(250).setData(null).setMsg(null);
	    	}
	        return ajaxJson;
	    }
	 
	 /**
	  * @description: 查询用户在活动中签到次数
	  * @param userId 用户id
	  * @param actId  活动id
	  * @return 
	  */
	 @ApiOperation(value = "查询用户在活动中签到次数", notes = "查询用户在活动中签到次数")
	 @PostMapping(value = "findSignInNumByUserIdAndActId")
	 public AjaxJson findSignInNumByUserIdAndActId(String userId, String actId){
		 try{
				Integer num = operatorUserSignInServiceIntegerface.findSignInNumByUserIdAndActId(userId, actId);
				ajaxJson.setSuccess(true).setStatus(200).setData(num).setMsg(null);
	    	}catch(Exception e){
	    		ajaxJson.setSuccess(false).setStatus(250).setData(null).setMsg(null);
	    	}
	        return ajaxJson;
	 }
	 
	 @ApiOperation(value = "查找所有签到配置", notes = "查找所有签到配置")
	    @PostMapping(value = "getAllOperatorUserSignInConfigurations")
	    public AjaxJson getAllOperatorUserSignInConfigurations() {
	    	try{
	    	List<OperatorUserSignInConfiguration>  configs=operatorUserSignInServiceIntegerface.selectOperatorUserSignInConfigurationlist();
	    	ajaxJson.setSuccess(true).setStatus(200).setData(configs).setMsg(null);
	    	}catch(Exception e){
	    		e.printStackTrace();
	    		ajaxJson.setSuccess(true).setStatus(250).setData(null).setMsg(null);
	    	}
	        return ajaxJson;
	    }
	 /**
	  * 如果要查询所有活动status参数传null或""
	  * 如果要查询历史活动status传参数1;或者只要不是null和""就行
	 * @Title: getAllOperatorUserSignInActives 
	 * @Description: TODO(这里用一句话描述这个方法的作用) 
	 * @param page
	 * @param size
	 * @param status
	 * @return    设定文件 
	 * @return AjaxJson    返回类型 
	 * @throws 
	  */
	 @ApiOperation(value = "查找所有签到活动和历史签到活动", notes = "查找所有签到活动和历史签到活动")
	    @PostMapping(value = "getAllOperatorUserSignInActives")
	    public AjaxJson getAllOperatorUserSignInActives(Integer page,Integer size,String status) {
	    	try{
	    	Map remap=operatorUserSignInServiceIntegerface.selectOperatorUserSignInActiveList(page, size,status);
	    	ajaxJson.setSuccess(true).setStatus(200).setData(remap).setMsg(null);
	    	}catch(Exception e){
	    		e.printStackTrace();
	    		ajaxJson.setSuccess(true).setStatus(250).setData(null).setMsg(null);
	    	}
	        return ajaxJson;
	    }
	 /**
	  * 查找活动人数签到排名
	  * 如果请求的是进行中的活动，signactivId传参数null或"",
	  * 如果请求的是具体哪个活动，signactivId传参数传该活动的id
、
	 * @Title: getAllOperatorUserSignIn 
	 * @Description: TODO(这里用一句话描述这个方法的作用) 
	 * @param page
	 * @param size
	 * @param signactivId
	 * @return    设定文件 
	 * @return AjaxJson    返回类型 
	 * @throws 
	  */
	 @ApiOperation(value = "查找活动人数签到排名", notes = "查找活动人数签到排名")
	    @PostMapping(value = "getAllOperatorUserSignIn")
	    public AjaxJson getAllOperatorUserSignIn(Integer page,Integer size,String signactivId) {
	    	try{
	    	Map remap=operatorUserSignInServiceIntegerface.selectOperatorUserSignInList(page, size,signactivId);
	    	ajaxJson.setSuccess(true).setStatus(200).setData(remap).setMsg(null);
	    	}catch(Exception e){
	    		e.printStackTrace();
	    		ajaxJson.setSuccess(true).setStatus(250).setData(null).setMsg(null);
	    	}
	        return ajaxJson;
	    }
	 
	 @ApiOperation(value = "编辑活动和签到规则", notes = "编辑活动和签到规则")
	    @PostMapping(value = "getOperatorUserSignInAcAndcon")
	    public AjaxJson getOperatorUserSignInAcAndcon(String id) {
	    	try{
	    	Map remap=operatorUserSignInServiceIntegerface.selectOperatorAcAndCon(id);
	    	ajaxJson.setSuccess(true).setStatus(200).setData(remap).setMsg(null);
	    	}catch(Exception e){
	    		e.printStackTrace();
	    		ajaxJson.setSuccess(true).setStatus(250).setData(null).setMsg(null);
	    	}
	        return ajaxJson;
	    }
	 
	 @ApiOperation(value = "查询签到排名开关状态", notes = "查询签到排名开关状态")
	    @PostMapping(value = "getOperatorUserSignInSwitch")
	    public AjaxJson getOperatorUserSignInSwitch() {
	    	try{
	    	List<OperatorUserSignInSwitch> switchs=operatorUserSignInServiceIntegerface.selectOperatorUserSignInSwitch();
	    	
	    	ajaxJson.setSuccess(true).setStatus(200).setData(switchs).setMsg(null);
	    	}catch(Exception e){
	    		e.printStackTrace();
	    		ajaxJson.setSuccess(true).setStatus(250).setData(null).setMsg(null);
	    	}
	        return ajaxJson;
	    }

	 
	 @ApiOperation(value = "更改签到排名开关状态", notes = "更改签到排名开关状态")
	    @PostMapping(value = "saveOperatorUserSignInSwitch")
	    public AjaxJson saveOperatorUserSignInSwitch(OperatorUserSignInSwitch operatorUserSignInSwitch) {
	    	try{
	    		OperatorUserSignInSwitch ss=operatorUserSignInServiceIntegerface.saveOperatorUserSignInSwitch(operatorUserSignInSwitch);
	    	
	    	ajaxJson.setSuccess(true).setStatus(200).setData(ss).setMsg(null);
	    	}catch(Exception e){
	    		e.printStackTrace();
	    		ajaxJson.setSuccess(true).setStatus(250).setData(null).setMsg(null);
	    	}
	        return ajaxJson;
	    }
	 
	 
	 @ApiOperation(value = "签到前端初始化接口", notes = "签到前端初始化接口")
	    @PostMapping(value = "saveOperatorUserSignInInitialize")
	    public AjaxJson saveOperatorUserSignInInitialize(String usrerid) {
	    	try{
	    		Map remap=operatorUserSignInServiceIntegerface.selectSignInInitialize(usrerid);
	    	
	    	ajaxJson.setSuccess(true).setStatus(200).setData(remap).setMsg(null);
	    	}catch(Exception e){
	    	
	    		ajaxJson.setSuccess(true).setStatus(250).setData(null).setMsg(null);
	    		e.printStackTrace();
	    	}
	        return ajaxJson;
	    }
	 
	 @ApiOperation(value = "进行中的活动人排名", notes = "进行中的活动人排名")
	    @PostMapping(value = "saveOperatorUserSignInAll")
	    public AjaxJson saveOperatorUserSignInAll() {
	    	try{
	    		Page<OperatorUserSignIn> signs=operatorUserSignInServiceIntegerface.selectOperatorUserSignInallList();
	    	
	    	ajaxJson.setSuccess(true).setStatus(200).setData(signs).setMsg(null);
	    	}catch(Exception e){
	    		e.printStackTrace();
	    		ajaxJson.setSuccess(true).setStatus(250).setData(null).setMsg(null);
	    	}
	        return ajaxJson;
	    }
	 /**
	  * 
	     * @Title: selectDefaultAddress  
	     * @Description: 根据userID查询是否有默认地址，有则返回
	     * @param @param userid
	     * @param @return      
	     * @return AjaxJson      
	     * @throws
	  */
	 @ApiOperation(value = "查询是否有默认地址", notes = "查找是否有默认地址")
	 @PostMapping(value = "selectDefaultAddress")
	 public AjaxJson selectDefaultAddress(String userid) {
		 try {
			 ajaxJson.setSuccess(true).setStatus(200).setData(operatorUserSignInServiceIntegerface.selectDefaultAddress(userid)).setMsg("操作成功");
		} catch (Exception e) {
			ajaxJson.setSuccess(true).setStatus(250).setData(null).setMsg("操作失败");
		}
		 return ajaxJson;
	 }
	/* @ApiOperation(value = "保存默认地址", notes="保存默认地址")
	 @PostMapping(value="saveDefaultAddress")
	 public AjaxJson saveDefaultAddress(@RequestBody PersonalAddress personalAddress) {
		 try {
			 ajaxJson.setSuccess(true).setStatus(200).setData(operatorUserSignInServiceIntegerface.saveDefaultAddress(personalAddress)).setMsg("操作成功");
		} catch (Exception e) {
			ajaxJson.setSuccess(true).setStatus(250).setData(null).setMsg("操作失败");
		}
		 return ajaxJson;
	 }*/
}
